<?
/**********************************************************************************************
Function		TEXT表单值初始化，过滤特殊字符
**********************************************************************************************/
function Init_TextValue($str)
{
	return str_replace(array("\""),array("&quot;"),$str);
}
/**********************************************************************************************
Function		格式化日期函数
Author			warmy
Careat Date		2006-12-21
parameter		$date			待格式化日期变量
				$format			格式化的类别
								默认为	0		只取日期
										1		取日期和时间
				$timestamp		指定$date的类型
								默认值	0		WINDOW 日期格式
										1		UNIX时间戳
**********************************************************************************************/
function Format_Date($date,$format=0,$timestamp = 0)
{
	$retstr		= "";
	if($date == '' || $date == 0)
		return $retstr;
	if($timestamp)
	{
		if($format)
			return $date;
		else
		{
			$retstr = explode($date," ");
			$retstr = $retstr[0];
		}
	}
	else
	{
		if($format)
			$retstr = date("Y-m-d H:i:s",$date);
		else
			$retstr = date("Y-m-d",$date);
	}
	return $retstr;
}
/**********************************************************************************************
Function		解析URL的POST数据，
				$LOCATIONURL					脚本文件名称
**********************************************************************************************/
function parse_get_data($LOCATIONURL="")
{
	$str = "";
	$arg_list = func_get_args();
	//如果url为空，则设置为当前页
	foreach($_GET as $key=>$value)
	{
		$ret = array_search($key,$arg_list);
		if(is_null($ret) || !$ret && !is_int($ret))
			$str .= "&$key=$value";
	}
	if($LOCATIONURL != "")
	{
		$path = pathinfo($LOCATIONURL);
		$path["basename"] = ereg_replace("(.[^\?]*)(.*)","\\1",$path["basename"]);
		if(substr($str,0,1) == "&")
			$str = substr($str,1);
		$str = $path["basename"]."?".$str;
	}
	return $str;
}
/**********************************************************************************************
Function		转换HTML文本到text
				$text					待处理字符串	
**********************************************************************************************/
function htm2text($text="")
{
	return str_replace(array('&', '<', '>', '"', "'"), array('&amp;', '&lt;', '&gt;', '&quot;', '&#039;'), $text);
}
/**********************************************************************************************
Function		转换TEXT文本到HTML
				$text					待处理字符串	
**********************************************************************************************/
function text2htm($text="")
{
	return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&#039;'), array('&', '<', '>', '"', "'"), $text);
}
function warmy_hash()
{
	$str = "abcedefhijklmnopqrestuvwxyz01234567890";
	$hash = date("Ymd");
	for($i=0;$i<8;$i++)
	{
		$hash .= substr($str,rand(0,35),1);
	}
	return $hash;
}
//简易加密算法
function WEncrypt($str)
{
	$retval		= "";
	for($i=0;$i<strlen($str);$i++)
	{
		$c		= ord(substr($str,$i,1));
		$h		= $c & 0xf0;
		$h		= $h >> 4;
		$l		= $c & 0x0f;
		$l		= $l << 4;
		$retval.= chr($h | $l);
	}
	return $retval;
}
//读取邮件发送配置
function SMTPConfig($system = true)
{
	global $db;
	$row	= array('smtp'=>'','port'=>'','username'=>'','sender'=>'','pwd'=>'');
	//读取当前用户是否有自己的配置信息
	if(!$system)
	{
		$sql = "select smtp,port,username,sender,pwd  from mailparams where owner='{$_SESSION['REEYEE_MANAGE']['user']}'";
		if(($query = $db->query($sql))===true)
		{
			if(!$row = mysql_fetch_array($query,MYSQL_ASSOC))
			{
				$sql = "select smtp,port,username,sender,pwd  from mailparams where id=1";
				$query = $db->query($sql);
				$row = mysql_fetch_array($query,MYSQL_ASSOC);
			}
		}
		else
			die("读取用户邮件配置信息失败！");
	}
	else
	{
		$sql = "select smtp,port,username,sender,pwd  from mailparams where id=1";
		$query = $db->query($sql);
		$row = mysql_fetch_array($query,MYSQL_ASSOC);
	}
	$row['pwd']	= WEncrypt($row['pwd']);
	return $row;
}
//读取邮件模板
function MAILTemplate($system = true)
{
	global $db;
	$row	= array('subject'=>'','body'=>'');
	//读取当前用户是否有自己的配置信息
	if(!$system)
	{
		$sql = "select subject,body  from mailtemplate where owner='{$_SESSION['REEYEE_MANAGE']['user']}'";
		if(($query = $db->query($sql))==true)
		{
			if(!$row = mysql_fetch_array($query,MYSQL_ASSOC))
			{
				$sql = "select subject,body  from mailtemplate where id=1";
				$query = $db->query($sql);
				$row = mysql_fetch_array($query,MYSQL_ASSOC);
			}
		}
		else
			die("读取用户邮件配置信息失败！");
	}
	else
	{
		$sql = "select subject,body  from mailtemplate where id=1";
		$query = $db->query($sql);
		$row = mysql_fetch_array($query,MYSQL_ASSOC);
	}
	return $row;
}
//判断当前客户是否具有操作潜在客户资料的权限
function CheckLCustomer($id)
{
	global $db;
	$retval		= false;
	$sql = "select id from lcustomer where id='$id' and del=0 and (owner='{$_SESSION['REEYEE_MANAGE']['user']}' or exists(SELECT username
FROM username where FIND_IN_SET('{$_SESSION["REEYEE_MANAGE"]["user"]}',boss) and lcustomer.owner=username.username))";

	if(($query = $db->query($sql))==true)
	{
		if(($row = mysql_fetch_array($query,MYSQL_ASSOC))==true)
		{
			if(count($row) == 1)
				$retval	= true;
		}
	}
	return $retval;
}



function wnews_GetPublish_URL($typeid)
{
	$url	= "";
	$sql	= "select url from function where id='$typeid'";
	$row	= $GLOBALS['db']->query_row($sql,MYSQL_NUM);
	while(list(,$value) = each($row))
		$url	= "/".trim(dirname($value[0]),"/") . "/";
	return $url;
}

/**
 * 判断管理员是否登录
 *
 */
function check_manage_login()
{
	if(!isset($_SESSION['REEYEE_MANAGE']) || 
		!isset($_SESSION['REEYEE_MANAGE']['user']) || 
		$_SESSION['REEYEE_MANAGE']['user'] == '')
		{
			$url = "login.php";
			header("Location: $url");
			exit;
			ANotify::halt("您还没有登录！",$url,$url);
		}
}
/**
 * 判断当前用户是否具有操作该功能的权限
 * Author			peace@warmy.cn
 * Create Date		2009-03-09
 * 					return 			void
 */
function check_manage_right()
{
	$GLOBALS['USER_RIGHT'] = cache_read("manage_{$_SESSION['REEYEE_MANAGE']['userid']}.php",DOCUMENTROOT."/cache/admin/");
	if(!$GLOBALS['USER_RIGHT'])
	{
		//读取用户权限信息
		$ur = new warmy_right($GLOBALS['db'],$_SESSION['REEYEE_MANAGE']['userid'],'U',false,RIGHTTABLE,GROUPTABLE);
		$GLOBALS['USER_RIGHT'] = $ur->get_WARMY_RIGHT();
		//初始化当前用户的功能树
		$user_tree	= new Warmy_tree($GLOBALS['db'],$GLOBALS['USER_RIGHT']['RIGHTS'],true,FUNCTIONTABLE);
		$GLOBALS['USER_RIGHT']['TREE'] = $user_tree->Get_Tree();
		//写入缓存信息
		cache_write("manage_{$_SESSION['REEYEE_MANAGE']['userid']}.php",$GLOBALS['USER_RIGHT'],DOCUMENTROOT."/cache/admin/");
	}
	if($GLOBALS['ATHENA_MODEL']>0)
	{
		if(!check_manage_right_micro($GLOBALS['ATHENA_MODEL']))
			ANotify::halt("您不具有访问该功能的权限！",ATHENA_SCRIPT_INDEX,ATHENA_SCRIPT_INDEX);
	}
}
/**
 * 判断是否具有对当前功能操作的具体权限
 *
 * @param unknown_type $fun			功能编号
 * @param unknown_type $micro		微权限值
 * @return unknown
 */
function check_manage_right_micro($fun,$micro = 0)
{
	$bret	= false;
	if(array_key_exists($fun,$GLOBALS['USER_RIGHT']['RIGHTS']))
	{
		if($micro>0)
		{
			if(($GLOBALS['USER_RIGHT']['RIGHTS'][$fun] & $micro) == $micro)
				$bret	= true;
		}
		else 
			$bret	= true;
	}
	return $bret;
}
/**
 * 加载页面内容
 * Author		peace@warmy.cn
 * Create Date	2009-03-09
 *
 */
function Athena_Template()
{
	$content = ob_get_contents();
	ob_clean();
	$charset = CHARSET;
	$script_index = ATHENA_SCRIPT_INDEX;
	$out_str = <<< EOT
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=$charset" />
<title>锐意先行-网站管理系统</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/std.js"></script>
<script type="text/javascript" src="js/admin.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="main">
  <tr class="rLeft">
    <td valign="top" class="leftBorder"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td valign="top"  class="logo"></td>
        </tr>
        <tr>
          <td valign="top" class="menuTop"><div></div></td>
        </tr>
      </table>
      <table width="100%" border="0" cellspacing="0" cellpadding="0"  class="menu" id="menu" >
      {$GLOBALS['USER_RIGHT']['TREE']['TREE']}
      </table></td>
    <td rowspan="3" valign="top" class="rRight"><div class="siteTitle">&nbsp;</div>
      <div class="dc" id="dc"><a href="{$script_index}?Model=6" class="mod white">修改密码</a>
      <a href="logout.php" class="logout whites">登出</a></div>
      <div class="dl"></div>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="content">
        <tr>
          <td height="42" valign="middle" class="nav"> ○ <a href="{$script_index}" style="color:white">HOME</a> >> {$GLOBALS['USER_RIGHT']['TREE'][$GLOBALS['ATHENA_MODEL']]['PATH']}</td>
        </tr>
		<form action="{$script_index}?Model={$GLOBALS['ATHENA_MODEL']}" method="post" name="frmList">
        <tr>
          <td valign="top">$content</td>
        </tr>
		</form>
      </table></td>
  </tr>
  <tr>
    <td valign="top" class="rLeft"></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="bottom">
  <tr>
    <td class="br"><span></span>&copy; Copyright  2005-2008  <a href="http://www.reeyee.cn" target="_blank">reeyee</a>  allrights revered.</td>
  </tr>
</table>
</body>
</html>

EOT;
	print($out_str);
}
function Add_Op_Log($op)
{
	$username 	= isset($_SESSION['REEYEE_MANAGE']['user']) && $_SESSION['REEYEE_MANAGE']['user'] != '' ? $_SESSION['REEYEE_MANAGE']['user'] : '匿名';
	$username	= isset($_SESSION['REEYEE_MANAGE']['realname']) && $_SESSION['REEYEE_MANAGE']['realname']!= '' ? "{$username}（{$_SESSION['REEYEE_MANAGE']['realname']}）":$username;
	$active		= date("Y-m-d H:i:s");
	$ip			= ip();
	$sql = "insert into ".WWCMSLOGTABLE."(username,op,active,ip) values('$username','$op','$active','$ip')";
	$GLOBALS['db']->query($sql);
}
?>